package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bjtime.designer.QRCode
import com.bjtime.designer.R
import com.bjtime.designer.attributes.*
import com.bjtime.designer.utils


class BarCodeView2 : BaseLabelView , View.OnTouchListener{

    var inflater : LayoutInflater
    var mLayoutView : View
    var mBorderView : View
    var mWBtn       : View
    var mHBtn       : View
    val TagViewName = "BarCodeView2"
    var des_text  = "10102110"
    var BRImage : ImageView? = null
    var des_image: Bitmap? = null
    var des_scale : Int = 0
    var mTextView : TextView? = null // barcodeview2_label_textview

    fun setText (s : String?, isUpdate : Boolean = true) {
        if (s != null || !s.equals(""))
            des_text = s!!
            mTextView!!.setText(des_text)
        if (isUpdate) {
            Log.e("VolDev", String.format("width:%d,  height:%d", width, height))
            if (width == 0 || height == 0)
                des_image = QRCode.createBRCode(des_text!!, utils.dp2px(context, 150f), utils.dp2px(context, 70f), false, Color.BLACK, utils.sp2px(context, 10f).toFloat())
            else
                des_image = QRCode.createBRCode(des_text!!, width, height, false, Color.BLACK, utils.sp2px(context, 10f).toFloat())
            BRImage!!.setImageBitmap(des_image)
            mTextView!!.setText(des_text)
        }

    }

    @JvmOverloads
    constructor(context: Context,content : String, attrs: AttributeSet ? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr) {
        des_viewtype = ViewAttr.VIEW_TYPE.BARCODE
        des_text=content
        inflater = LayoutInflater.from(this.context)
        mLayoutView = inflater.inflate(R.layout.layout_designer_label_barcodeview2, null)
        this.addView(mLayoutView)
        BRImage = mLayoutView.findViewById<ImageView>(R.id.barcodeview2_label_qrimageview)
        mBorderView = mLayoutView.findViewById<View>(R.id.barcodeview2_label_broderview)
        mWBtn = mLayoutView.findViewById(R.id.barcodeview2_label_right_btn)
        mHBtn = mLayoutView.findViewById(R.id.barcodeview2_label_bottom_btn)
        if (width == 0 || height == 0)
            des_image = QRCode.createBRCode(des_text!!, utils.dp2px(context, 150f), utils.dp2px(context, 70f), false, Color.BLACK, utils.sp2px(context, 10f).toFloat())
        else
            des_image = QRCode.createBRCode(des_text!!, width, height, false, Color.BLACK, utils.sp2px(context, 10f).toFloat())
        BRImage!!.setImageBitmap(des_image)


        setSelect (false)
        mTextView = mLayoutView.findViewById(R.id.barcodeview2_label_textview)
        mTextView!!.setText(des_text)

        mWBtn.setOnTouchListener(this)
        mHBtn.setOnTouchListener(this)
    }


    private var xDelta: Float = 0f
    private var yDelta: Float = 0f

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                xDelta = x - super.getWidth()
                yDelta = y - super.getHeight()
                super.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE -> {
                xDelta = width.toFloat()
                yDelta = height.toFloat()

                val params = super.getLayoutParams()
                val xDistance = x + xDelta
                val yDistance = y + yDelta
                if (v.id == mWBtn.id) {
                    params.width = xDistance.toInt()
                } else {
                    params.height = yDistance.toInt()
                }
                super.setLayoutParams(params)
            }
            MotionEvent.ACTION_UP -> super.requestDisallowInterceptTouchEvent(false)
        }
        return true
    }

    override fun setSelect(select: Boolean) {
        super.setSelect(select)
        if (!select) {
            mBorderView.setBackgroundResource(0)
            mWBtn.visibility = View.GONE
            mHBtn.visibility = View.GONE
        } else {
            if (!des_lock) {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border)
                mWBtn.visibility = View.VISIBLE
                mHBtn.visibility = View.VISIBLE
            } else {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border_locked)
                mWBtn.visibility = View.GONE
                mHBtn.visibility = View.GONE
            }
        }
    }

    override fun getAttr (): BarCodeAttr {
        var attr  = BarCodeAttr ();
        attr.des_id = des_id
        attr.des_left = des_left
        attr.des_top = des_top
        attr.des_width = des_width
        attr.des_height = des_height
        attr.des_lock = des_lock
        attr.des_ptype = des_ptype
        attr.des_rotate = des_rotate
        attr.des_reversed = des_reversed
        attr.des_viewtype = ViewAttr.VIEW_TYPE.BARCODE
        attr.des_image = utils.Bitmap2ByteArray (des_image!!)
        attr.des_text = des_text
        attr.des_scale = des_scale
        return attr
    }

    override fun setAttr (attr: BaseAttr?) {
        attr as BarCodeAttr
        des_id = attr.des_id
        des_left = attr.des_left
        des_top = attr.des_top
        des_width = attr.des_width
        des_height = attr.des_height
        des_lock = attr.des_lock
        des_ptype = attr.des_ptype
        des_rotate = attr.des_rotate
        des_reversed = attr.des_reversed
        des_viewtype = attr.des_viewtype
        des_image =  BitmapFactory.decodeByteArray(attr.des_image, 0, attr.des_image!!.size)
        des_scale = attr.des_scale
        des_text = attr.des_text
    }

    override fun toString(): String {
        return "ViewName    : $TagViewName"
    }



    fun setTextSize (size:Int) {
        mTextView!!.textSize = size.toFloat()
    }

    fun getTextSize () : Int {
        return mTextView!!.textSize.toInt()
    }


    override fun prepared() {
        setText (des_text)
    }
}